#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>
#include <QThread>
#include <QDebug>
#include <QDateTime>
#include <QMap>
#include <QString>
#include <QTcpServer>
#include <QTcpSocket>
#include <QUdpSocket>

#include <iostream>

#ifndef _WORKER_H_
#define _WORKER_H_

class MainWindow;

class Worker : public QObject {
    Q_OBJECT

public slots:
    // void doWork(QTcpSocket *socket, QByteArray &m_data, uint16_t *bin1, uint16_t *bin2, uint16_t *bin3, uint16_t *bin4) {
    void doWork(QTcpSocket *socket, uint16_t *bin1, uint16_t *bin2, uint16_t *bin3, uint16_t *bin4) {
    // void doWork(QTcpSocket *socket) {
	// void doWork(QTcpSocket *socket, int array[5] ) {

        // if (!socket->waitForReadyRead(1000)) {
        //     std::cout << "Timeout or error:" << socket->errorString().toStdString();
        //     return;
        // }

        // QByteArray data;
        // data.clear();
        while( socket && socket->state() == QTcpSocket::ConnectedState ) {
        
            QByteArray data = socket->readAll();
            // QString cmd = QString(data);
            // // QString cmd = QString(data).remove("\n");

            QString cmd;
            // if( !data.isEmpty() ){
            //     cmd = QString(data);
            // 	std::cout << "cmd: " << cmd.toUtf8().toStdString() << std::endl;
            // } else {
            //     std::cout << "QByteArray data is empty!\n";
            //     QThread::sleep(1);
            //     continue;
            // }

            // if( !m_data.isEmpty() ){
            if( !data.isEmpty() ){

                //cmd = QString(data);
                // cmd = QString::fromUtf8(m_data.trimmed());
                cmd = QString::fromUtf8(data.trimmed());
                // std::cout << "cmd: " << cmd.toUtf8().toStdString() << std::endl;

                if( cmd.size() <= 0 )
                    continue;

                // 输出到终端（确保编码正确）
                QTextStream out(stdout);
                out.setCodec("UTF-8");  // 设置控制台输出编码
                out << "cmd: " << cmd << Qt::endl;  // 使用Qt的文本流输出

                if( cmd == "1" ){
                    for( int i=0; i<5; ++i ){
                        bin1[i] = (i+1)*1;
                        // array[i] = i;
                    }
                }

                if( cmd == "2" ){
                    for( int i=0; i<5; ++i ){
                        bin2[i] = (i+1)*2;
                    }
                }

                if( cmd == "3" ){
                    for( int i=0; i<5; ++i ){
                        bin3[i] = (i+1)*3;
                    }
                }
                
                if( cmd == "4" ){
                    for( int i=0; i<5; ++i ){
                        bin4[i] = (i+1)*4;
                    }
                }

                // QThread::sleep(1);
                QString message = "this is a test message!" + QDateTime::currentDateTime().toString();
                if(socket->state() == QTcpSocket::ConnectedState) {
                    QByteArray msgData = message.toUtf8();  // 明确转换为UTF-8
                    socket->write(msgData);
                    //socket->write(message.toUtf8());
                    socket->flush();
                }

                // std::cout << "AAAAAA\n";

                QThread::yieldCurrentThread();

                for( int i=0; i<5; ++i ){

                    if( cmd == "1" )
                        // std::cout << "array[" << i << "] = " << array[i] << std::endl;
                        std::cout << "bin1[" << i << "] = " << bin1[i] << std::endl;

                    if( cmd == "2" )
                        std::cout << "bin2[" << i << "] = " << bin2[i] << std::endl;

                    if( cmd == "3" )
                        std::cout << "bin3[" << i << "] = " << bin3[i] << std::endl;

                    if( cmd == "4" )
                        std::cout << "bin4[" << i << "] = " << bin4[i] << std::endl;
                }

                // data.clear();
                 // m_data.clear();
                 // m_data = "";
            }
        }
        
        emit workFinished();
    }

signals:
    void workFinished();
    // void resultReady(const QString &result);

};


#endif // _WORKER_H_